package com.tbvtc.agriculturalcreditchainserver2025.mapper;

import com.tbvtc.agriculturalcreditchainserver2025.dto.Cart.*;
import com.tbvtc.agriculturalcreditchainserver2025.dto.consumer.addCartDto;
import com.tbvtc.agriculturalcreditchainserver2025.vo.Cart.*;
import com.tbvtc.agriculturalcreditchainserver2025.vo.ConsumerVo.addCartVo;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface CartMapper {
    @Insert("insert into cart (user_id,good_id,num,phone,time) values (#{userId},#{goodId},#{num},#{phone},#{time})")
    void addCart(addCartDto addCartDto);

    @Select("select num from cart where id=#{id}")
    Integer selectNum(Integer id);

    @Update("update cart set num=#{num} where id=#{id}")
    Integer updateCartNum(Integer id, Integer num);

    @Select("select * from cart where user_id=#{userId} and good_id=#{goodId}")
    addCartVo selectCart(Integer userId, Integer goodId);

    @Select("select num from cart where id=#{id}")
    Integer selectCartNum(Integer id);

    @Delete("delete from cart where id=#{id}")
    Integer removeCart(Integer id);

    //    @Insert("insert into cart_user_address(user_id,address,username,phone,create_time,update_time) values (#{userId},#{address},#{username},#{phone},#{createTime},#{updateTime});")
    void addAddress(cartAddressDto cartAddressDto);

    @Delete("delete  from cart where id=#{id}")
    void clearCart(Integer id);


    List<cartPriceVo> selectCartIds(SettlementCartDto settlementCartDto);

    //    @Insert("insert into cart_order(user_id,total_price,state,time) values (#{userId},#{totalPrice},#{stats},#{time});")

    //    @Options(useGeneratedKeys = true, keyProperty = "id") // 获取自增主键
    void settlementCart(cartOrderVo cartOrderVo);

    @Select("select id from cart where user_id=#{userId}")
    List<Integer> selecIdByUserIdtCart(Integer userId);

    @Select("select id from cart_order where user_id=#{userId} and total_price=#{totalPrice}")
    Integer selectIdByUserIdtAndPrice(Integer userId, BigDecimal totalPrice);

    @Delete("delete from test where order_id=#{orderId}")
    void removeCartByOrderId(String orderId);


    Integer ClearCart(SettlementCartDto settlementCartDto);


    List<CartSelectPhoneAndGoodByUserId> selectCartPhoneAndGoodByUserId(SettlementCartDto settlementCartDto);

    //    @Select("select address_id, province, city, district, detail_address, receiver_name, receiver_phone, is_default from user_address where user_id=#{userId};")
    List<addressItem> selectAddressByUserId(Integer userId);

    @Insert("insert into test(order_id,phone,good_id,num,address,user_id,username,create_time,state) values (#{orderId},#{phone},#{goodId},#{num},#{address},#{userId},#{username},#{time},#{state});")
    void addOrder(CartSelectPhoneAndGoodByUserId s);

    @Select("select phone from user where id=#{id};")
    String selectPhoneById(Integer id);

    @Select("select username from user where id=#{userId};")
    String selectUsernameByUserId(Integer userId);

    // @Select("select * from test where phone=#{phone};")
//    @Select("select c.name,c.crop_image,c.create_time,c.land_name,c.price,t.num,t.address,t.time,t.username,t.state\n" +
//            "from crop c , test t where t.good_id=c.id and t.phone=#{phone}")

    List<GroupData> findOrderByPhone(String phone, String status);

    @Select("select id from cart where user_id=#{userId}")
    List<Integer> ClearCartGood(Integer userId);

    @Update("update test set state=1 where phone=#{phone}")
    void UpdateCartState(CartSelectPhoneAndGoodByUserId s);


    List<CartSelectUserByUserId> selectCartUserByUserId(Integer userId, Integer state);

//    @Select("select  t.id ,t.good_id, t.num,cr.price,cr.crop_image,cr.name,t.user_id from test t,crop cr where t.good_id=cr.id and t.state=0 and t.user_id=#{userId};")


    @Update("update test set state=1,create_time={now} where order_id=#{orderId}")
    Integer payOrder(String orderId, LocalDateTime now);

    //    @Select(" UPDATE user_address \n" +
//            "    SET is_default = 0 \n" +
//            "    WHERE user_id = #{userId} \n" +
//            "      AND is_default = 1")
    void updateUserAddressSetNonDefault(Integer userId);

    //  @Select("select * from test where order_id=#{orderId}")
    GroupedData findOrderByOrderId(String orderId);

    @Delete("delete from user_address where address_id=#{addressId}")
    void deleteAddress(Integer addressId);

    void updateAddress(CartUpdataAddressDto cartUpdataAddressDto);

    @Update("update test set express_id=#{expressId} ,express_company=#{expressCompany} ,state=2 where order_id=#{orderId} and state=1")
    Integer addLogisticsInfo(String orderId, String expressId, String expressCompany);

    @Select("select user_id from test where id=#{id}")
    String selectPhoneByOrderId(Integer id);

    @Select("select * from cart where user_id= #{userId}")
    List<addCartVo> getCartInfo(Integer userId);

    @Select("select receiver_name from user_address where user_id= #{userId} AND address_id=1")
    String selectAddressResName(Integer userId);
}
